package com.zhihuichongcheng.application.ui.coordination.activity;

import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.coordination.CoordinationFileBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.coordination.adapter.CoordinationAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class CoordinationListActivity extends BaseActivity{
    private XRecyclerView mRcvCoordination;
    private CoordinationAdapter mAdapter;
    private List<CoordinationFileBean> mListData = new ArrayList<>();
    private ImageView mImSearch;
    private String id;
    private int mPage =1;
    private int mPageSize = 15;
    private LinearLayout mLlNoData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordination);
        initView();
        initData();
    }

    private void initView()
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.coordination_headview,null);

        mLlNoData = (LinearLayout)findViewById(R.id.ll_nodata);
        setImRight(R.drawable.ic_download_list, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(DownloadManager.ACTION_VIEW_DOWNLOADS);
            }
        });
        mImSearch = (ImageView)view.findViewById(R.id.im_search);
        mImSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(CoordinationSearchActivity.class);
            }
        });
        mRcvCoordination = (XRecyclerView)findViewById(R.id.rcv_coordination);
        mRcvCoordination.addHeaderView(view);
        mAdapter = new CoordinationAdapter(mContext);
        mRcvCoordination.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvCoordination.setAdapter(mAdapter);
        mRcvCoordination.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage =1;
                loadData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });
    }

    private void initData()
    {

        Intent intent = getIntent();
        if(intent!=null&&intent.hasExtra("title"))
        {
            setTitle(intent.getStringExtra("title"));
        }
        if(intent!=null&&intent.hasExtra("id"))
        {
            id = intent.getStringExtra("id");
        }
        DialogUtil.showLoadDiadlog(mContext);
        loadData(mPage);
        mAdapter.setData(mListData);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemOnclick(new OnItemOnclick<CoordinationFileBean>() {
            @Override
            public void onClick(CoordinationFileBean coordinationFileBean, int position) {
                int type = coordinationFileBean.getFileType();
                if(type==1)
                {
                    Bundle bundle = new Bundle();
                    bundle.putString("title",coordinationFileBean.getFileName());
                    bundle.putString("id",coordinationFileBean.getFileContentId());
                    startActivity(CoordinationDetailActivity.class,bundle);
                }
                else
                {
                    Bundle bundle = new Bundle();
                    bundle.putString("title",coordinationFileBean.getFileName());
                    bundle.putString("id",coordinationFileBean.getFileContentId());
                    startActivity(CoordinationListActivity.class,bundle);
                }
            }
        });
    }
    private void loadData(final int page)
    {
        HashMap<String,String> params = new HashMap<>();
        params.put("token",mUser.getToken());
        params.put("directoryId",id);
        params.put("pageRows",""+mPageSize);
        params.put("curPage",""+page);
        mManager.post(NetworkAPI.Coordination_list, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(mContext);
                List<CoordinationFileBean> list = GsonUtil.paraeFromStringToList(value,CoordinationFileBean.class);
                if(page ==1)
                {
                    mAdapter.clear();
                    if(list.size()==0)
                    {
                        mLlNoData.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mLlNoData.getLayoutParams();
                        params.setMargins(0,(DeviceUtil.getHeight(mContext)-DeviceUtil.dip2px(mContext,50))/10*4-DeviceUtil.dip2px(mContext,119),0,0);
                        mLlNoData.setLayoutParams(params);
                        mRcvCoordination.setVisibility(View.GONE);
                    }
                    else
                    {
                        mLlNoData.setVisibility(View.GONE);
                        mRcvCoordination.setVisibility(View.VISIBLE);
                    }
                }
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvCoordination.loadMoreComplete();
                mRcvCoordination.refreshComplete();
                if(list.size()<mPageSize)
                {
                    mRcvCoordination.setNoMore(true);
                }
            }
            @Override
            public void onFailure(String value) {
                DialogUtil.dismiss(mContext);
                mLlNoData.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mLlNoData.getLayoutParams();
                params.setMargins(0,(DeviceUtil.getHeight(mContext)-DeviceUtil.dip2px(mContext,50))/10*4-DeviceUtil.dip2px(mContext,75),0,0);
                mLlNoData.setLayoutParams(params);
                mRcvCoordination.setVisibility(View.GONE);
            }
        },params);
    }
}
